import sys

import streamlit as st
import streamlit_antd_components as sac

from chatchat import __version__
from chatchat.server.utils import api_address
from chatchat.webui_pages.dialogue.dialogue import  dialogue_page
from chatchat.webui_pages.kb_chat import kb_chat
from chatchat.webui_pages.knowledge_base.knowledge_base import knowledge_base_page
from chatchat.webui_pages.utils import *

api = ApiRequest(base_url=api_address())

if __name__ == "__main__":
    is_lite = "lite" in sys.argv  # TODO: remove lite mode

    st.set_page_config(
        "OriginVoice言之有源",
        get_img_base64("chatchat_icon_blue_square_v2.png"),
        initial_sidebar_state="expanded",
        menu_items=None,  # 禁用Deploy菜单
        layout="centered",
    )

    # 获取竹子背景图的base64编码
    zhuzi_bg = get_img_base64("zhuzi.png")
    
    # use the following code to set the app to wide mode and the html markdown to increase the sidebar width
    st.markdown(
        f"""
        <style>
        /* 侧边栏最外层容器 - 设置背景图 */
        section[data-testid="stSidebar"] {{
            background: linear-gradient(rgba(255, 255, 255, 0.5), rgba(255, 255, 255, 0.7)), 
                        url('{zhuzi_bg}') left center/auto 100% no-repeat !important;
        }}
        
        /* 侧边栏所有内层容器设为透明 */
        section[data-testid="stSidebar"] > div,
        section[data-testid="stSidebar"] [data-testid="stSidebarContent"],
        section[data-testid="stSidebar"] [data-testid="stSidebarUserContent"] {{
            background-color: transparent !important;
            background-image: none !important;
        }}
        
        [data-testid="stSidebarUserContent"] {{
            padding-top: 20px;
        }}
        
        /* 移除组件的白色半透明背景，让它们完全透明融入背景 */
        section[data-testid="stSidebar"] .element-container {{
            background-color: transparent !important;
        }}
        
        /* 让某些重要组件有轻微背景便于阅读 */
        section[data-testid="stSidebar"] .stRadio,
        section[data-testid="stSidebar"] .stSelectbox,
        section[data-testid="stSidebar"] .stCheckbox {{
            background-color: rgba(255, 255, 255, 0.3) !important;
            border-radius: 6px;
            padding: 8px;
        }}
        
        /* 主内容区域背景 - 统一的淡雅背景 */
        .main .block-container {{
            padding-top: 25px;
            background: transparent;
            border-radius: 0;
        }}
        
        /* 整个主区域背景 - 统一背景色 */
        .main {{
            background: #fafdfb;
        }}
        
        [data-testid="stBottomBlockContainer"] {{
            padding-bottom: 20px;
            background: transparent;
        }}
        
        /* 欢迎标题样式优化 */
        .main h1, .main h2, .main h3 {{
            color: #2d5016;
        }}
        
        /* ==================== 输入框美化 ==================== */
        
        /* 主聊天输入框容器 */
        .stChatInputContainer {{
            background: white !important;
            border-radius: 16px !important;
            border: 2px solid #e0e0e0 !important;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08) !important;
            transition: all 0.3s ease !important;
            padding: 4px !important;
        }}
        
        /* 输入框聚焦效果 */
        .stChatInputContainer:focus-within {{
            border-color: #7cb342 !important;
            box-shadow: 0 4px 16px rgba(124, 179, 66, 0.2) !important;
            transform: translateY(-2px);
        }}
        
        /* 输入框内的文本区域 */
        .stChatInputContainer textarea {{
            font-size: 15px !important;
            line-height: 1.6 !important;
            padding: 12px 16px !important;
        }}
        
        /* 输入框占位符文字 */
        .stChatInputContainer textarea::placeholder {{
            color: #9e9e9e !important;
            font-style: italic;
        }}
        
        /* 聊天消息容器 */
        .stChatMessage {{
            background: rgba(255, 255, 255, 0.6) !important;
            border-radius: 12px !important;
            border: none !important;
            box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05) !important;
            margin: 8px 0 !important;
            transition: all 0.2s ease !important;
        }}
        
        .stChatMessage:hover {{
            box-shadow: 0 3px 8px rgba(0, 0, 0, 0.1) !important;
            transform: translateX(4px);
        }}
        
        /* ==================== 按钮美化 ==================== */
        
        /* 所有按钮基础样式 */
        .stButton > button {{
            border-radius: 10px !important;
            font-weight: 500 !important;
            transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1) !important;
            border: none !important;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1) !important;
        }}
        
        /* 按钮悬停效果 */
        .stButton > button:hover {{
            transform: translateY(-3px) scale(1.02);
            box-shadow: 0 6px 16px rgba(124, 179, 66, 0.3) !important;
        }}
        
        /* 按钮按下效果 */
        .stButton > button:active {{
            transform: translateY(-1px) scale(0.98);
            box-shadow: 0 2px 8px rgba(124, 179, 66, 0.2) !important;
        }}
        
        /* 主要操作按钮（蓝色） */
        .stButton > button[kind="primary"] {{
            background: linear-gradient(135deg, #7cb342 0%, #558b2f 100%) !important;
            color: white !important;
        }}
        
        .stButton > button[kind="primary"]:hover {{
            background: linear-gradient(135deg, #8bc34a 0%, #689f38 100%) !important;
        }}
        
        /* 次要按钮 */
        .stButton > button[kind="secondary"] {{
            background: white !important;
            border: 2px solid #7cb342 !important;
            color: #558b2f !important;
        }}
        
        .stButton > button[kind="secondary"]:hover {{
            background: #f1f8e9 !important;
        }}
        
        /* 发送按钮 - 保持简洁样式 */
        button[data-testid="stChatInputSubmitButton"] {{
            border-radius: 8px !important;
            transition: all 0.2s ease !important;
        }}
        
        button[data-testid="stChatInputSubmitButton"]:hover {{
            transform: scale(1.05);
            opacity: 0.9;
        }}
        
        button[data-testid="stChatInputSubmitButton"]:active {{
            transform: scale(0.95);
        }}
        
        /* ==================== 下载按钮美化 ==================== */
        
        .stDownloadButton > button {{
            background: linear-gradient(135deg, #42a5f5 0%, #1976d2 100%) !important;
            color: white !important;
        }}
        
        .stDownloadButton > button:hover {{
            background: linear-gradient(135deg, #64b5f6 0%, #1e88e5 100%) !important;
            transform: translateY(-3px) scale(1.02);
        }}
        
        /* 隐藏右上角的Deploy按钮和菜单 */
        .stDeployButton {{
            display: none !important;
        }}
        
        /* 隐藏右上角的汉堡菜单按钮 */
        [data-testid="stToolbar"] {{
            display: none !important;
        }}
        
        
        </style>
        """,
        unsafe_allow_html=True,
    )

    with st.sidebar:
        st.image(
            get_img_base64("logo-long-chatchat-trans-v2.png"), use_column_width=True
        )
        st.caption(
            f"""<p align="right">RAG问答系统</p>""",
            unsafe_allow_html=True,
        )

        selected_page = sac.menu(
            [
                sac.MenuItem("多功能对话", icon="chat"),
                sac.MenuItem("RAG 对话", icon="database"),
                sac.MenuItem("知识库管理", icon="hdd-stack"),
            ],
            key="selected_page",
            open_index=0,
        )

        sac.divider()

    if selected_page == "知识库管理":
        knowledge_base_page(api=api, is_lite=is_lite)
    elif selected_page == "RAG 对话":
        kb_chat(api=api)
    else:
        dialogue_page(api=api, is_lite=is_lite)
